package com.baidu.controller.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        //将前端界面获取的username添加带session域中
        req.getSession().setAttribute("username",username);
        //判断用户登录的用户名称是否为空
        if (username==null || "".equals(username))
        {
            resp.sendRedirect(getServletContext().getContextPath()+"/login.jsp");
            return;
        }
        //用户username不为空跳转到首页
        resp.sendRedirect(getServletContext().getContextPath()+"/index.jsp");

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
